# -*- coding: UTF-8  -*-
import sandbox.PG_PY as PG
import commons.util as util
import commons.dbutil as db

#--------------------------------------------------------------
def insertToDb():

    news=file("E:/news.txt","r")
    news_content=news.read()
    news.close()

    newsFormat=news_content.split("，")
    print "the newsFormat has %d object" % len(newsFormat)
    
    obj=util.getObj()
    obj.title="news from sina"
    obj.create_time='now'

    for each in newsFormat:
        obj.content=each
        PG.create_u(obj)

#----------------------------------------------------------------------
def dbChange():
    conn=db.getconn(PG.conn_cfg);
    newId=PG.queryId()

    for id in newId:
        newDiary=db.read(conn,PG.tb_diary,id.id)
    
        Format=unicode(newDiary.content,"UTF-8")

        if len(Format)<10:
            newDiary.title=newDiary.content
            db.save(conn,PG.tb_diary, newDiary)
        
        else:
            newDiary.title=Format[:10]
            db.save(conn,PG.tb_diary, newDiary)
        
    db.submit(conn)


dbChange()